import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:encrypt_and_decrypt_files/encrypt_page/encrypt_decrypt_page.dart';
import 'package:encrypt_and_decrypt_files/my/register_page.dart';
import 'package:encrypt_and_decrypt_files/widget/extension.dart';
import 'package:encrypt_and_decrypt_files/widget/theme_provider.dart';
import 'package:flustars_flutter3/flustars_flutter3.dart';

class EncryptPage extends StatefulWidget {
  const EncryptPage({super.key});

  @override
  _EncryptPageState createState() => _EncryptPageState();
}

class _EncryptPageState extends State<EncryptPage>
    with SingleTickerProviderStateMixin {
  int selectEncryptOrDecryptType = 0;
  String? memberType;

  @override
  void initState() {
    super.initState();
    UserModel userModel = SpUtil.getObj('userModel', (map) {
      return UserModel.fromJson(map as Map<String, dynamic>);
    })!;
    memberType = userModel.memberType;
  }

  @override
  Widget build(BuildContext context) {
    final themeProvider = Provider.of<ThemeProvider>(context);
    return Scaffold(
      appBar: AppBar(
        title: Text('Main Page'.t(context)),
        backgroundColor: themeProvider.mainColor,
        centerTitle: true,
      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          const SizedBox(
            height: 20,
          ),
          Container(
            padding: const EdgeInsets.symmetric(vertical: 20.0),
            margin: const EdgeInsets.symmetric(horizontal: 30),
            decoration: BoxDecoration(
              color: themeProvider.mainColor,
              borderRadius: BorderRadius.circular(15),
              boxShadow: [
                BoxShadow(
                  color: Colors.grey.withOpacity(0.5),
                  spreadRadius: 5,
                  blurRadius: 7,
                  offset: const Offset(0, 3), // changes position of shadow
                ),
              ],
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                const Icon(Icons.account_circle, color: Colors.grey),
                const SizedBox(width: 10),
                Text(
                  '${'Account Status'.t(context)}: $memberType',
                  style: const TextStyle(fontSize: 18),
                ),
              ],
            ),
          ),
          const SizedBox(
            height: 30,
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              _buildButton(
                  context, 'Encrypt'.t(context), Icons.lock, 0, themeProvider),
              const SizedBox(
                width: 20,
              ),
              _buildButton(context, 'Decrypt'.t(context), Icons.lock_open, 1,
                  themeProvider),
            ],
          ),
          const SizedBox(
            height: 30,
          ),
          GestureDetector(
            behavior: HitTestBehavior.opaque,
            onTap: () {
              Navigator.push(
                  context,
                  MaterialPageRoute(
                      builder: (_) => EncryptDecryptPage(
                            isEncrypting: selectEncryptOrDecryptType == 0,
                          )));
            },
            child: Container(
              padding: const EdgeInsets.all(10),
              decoration: BoxDecoration(
                color: themeProvider.mainColor,
                borderRadius: BorderRadius.circular(10),
                boxShadow: [
                  BoxShadow(
                    color: Colors.grey.withOpacity(0.5),
                    spreadRadius: 2,
                    blurRadius: 5,
                    offset: const Offset(0, 3),
                  ),
                ],
              ),
              child: Text(
                'Next Step'.t(context),
                style: const TextStyle(color: Colors.black),
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildButton(BuildContext context, String text, IconData icon,
      int type, ThemeProvider themeProvider) {
    return GestureDetector(
      onTap: () {
        setState(() {
          selectEncryptOrDecryptType = type;
        });
      },
      child: Container(
        padding: const EdgeInsets.all(10),
        margin: const EdgeInsets.symmetric(horizontal: 10),
        decoration: BoxDecoration(
          color: selectEncryptOrDecryptType == type
              ? themeProvider.mainColor
              : Colors.white,
          borderRadius: BorderRadius.circular(10),
          border: Border.all(
              color: selectEncryptOrDecryptType == type
                  ? themeProvider.mainColor
                  : Colors.grey),
        ),
        child: Row(
          mainAxisSize: MainAxisSize.min,
          children: [
            Icon(icon,
                color: selectEncryptOrDecryptType == type
                    ? Colors.black
                    : Colors.grey),
            const SizedBox(width: 10),
            Text(
              text,
              style: TextStyle(
                  color: selectEncryptOrDecryptType == type
                      ? Colors.black
                      : Colors.grey),
            ),
          ],
        ),
      ),
    );
  }
}
